package com.china08.yxyapi.api.manage.sch.platform.website.image;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.sws.SwsAlbums;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.YxySwsAlbumsRepository;

@CrossOrigin
@RestController("SwsAlbumsListController")
public class SwsAlbumsListController extends YxyApiBaseController {

    @Autowired
    YxySwsAlbumsRepository yxySwsAlbumsRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/swsalbums/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String schoolId = getProfile(request, response).getSchoolId();
        Pageable pageable = reqModel.getPageable(Direction.ASC, "albumsDate");
        Page<SwsAlbums> page = yxySwsAlbumsRepository.findBySchoolId(schoolId, pageable);

        List<Albums> albumList = new ArrayList<Albums>();

        List<SwsAlbums> list = page.getContent();
        for (SwsAlbums swsAlbums : list) {
            String id = swsAlbums.getId();
            String name = swsAlbums.getName();
            String covers = swsAlbums.getCovers();
            Integer imageCount = swsAlbums.getImageCount();
            if (name.length() > 10) {
                name = name.substring(0, 10) + "...";
            }
            Albums albums = new Albums();
            albums.id = id;
            albums.name = name;
            albums.covers = covers;
            albums.imageCount = imageCount;

            albumList.add(albums);
        }
        RespModel respModel = new RespModel();
        respModel.list = albumList;
        respModel.totalElements = page.getTotalElements();
        respModel.number = page.getNumber();
        return respModel;
    }

    public static class Albums {
        public String id;
        public String name;
        public String covers;
        public Integer imageCount;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
    }

    protected static class RespModel extends YxyApiRespModel {
        public List<Albums> list;
        public long totalElements;
        public long number;
    }
}
